﻿using AutoMapper;
using MediatR;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02.Leo
{
    /// <summary>
    /// 磅秤类型
    /// </summary>
    public class ScaleTypeCreateHandler : IRequestHandler<ScaleTypeCreateCommend, Result>
    {
        private readonly IBaseRepository<ScaleType> ScaleType;
        private readonly IMapper mapper;

        public ScaleTypeCreateHandler(IMapper mapper, IBaseRepository<ScaleType> ScaleType)
        {
            this.mapper = mapper;
            this.ScaleType = ScaleType;
        }

        public async Task<Result> Handle(ScaleTypeCreateCommend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                ScaleType scaleType = mapper.Map<ScaleType>(request);
                var res = ScaleType.Add(scaleType);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
